package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoomServiceImpl extends BaseService implements IRoomService {

    @Override
    public void add(RoomBean room) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        mapper.addRoom(room);
        session.commit();
        session.close();
    }

    @Override
    public boolean del(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        if(mapper.countStudentByRoom(id) == 0){
            IRoomMapper mapper1 = session.getMapper(IRoomMapper.class);
            IFacilityMapper mapper2 = session.getMapper(IFacilityMapper.class);
            mapper2.delFacility(id);
            mapper1.delRoom(id);
            session.commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }

    @Override
    public RoomBean findById(int id) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        IStudentMapper mapper1 = session.getMapper(IStudentMapper.class);
        RoomBean room = mapper.findById(id);
        room.setInNum(mapper1.countStudentByRoom(id));
        room.setList(mapper1.findByRoom(id));
        session.close();
        return room;
    }

    @Override
    public List<RoomBean> findByItem(String address, String state, String liveRoom, String roomType) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        IStudentMapper mapper1 = session.getMapper(IStudentMapper.class);
        List<RoomBean> list = mapper.findByItem(address,state,liveRoom,roomType);
        for(RoomBean r : list){
            r.setInNum(mapper1.countStudentByRoom(r.getId()));
        }
        session.close();
        return list;
    }

    @Override
    public CutPageBean<RoomBean> cutByItem(int pageNO, String address, String state,
                                           String liveRoom, String roomType) {
        CutPageBean<RoomBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        IStudentMapper mapper1 = session.getMapper(IStudentMapper.class);
        cutBean.setList(mapper.cutItemList((pageNO-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE,address,
                state,liveRoom,roomType));
        for(RoomBean r : cutBean.getList()){
            r.setInNum(mapper1.countStudentByRoom(r.getId()));
        }
        cutBean.setCount(mapper.cutItemCount(address,state,liveRoom,roomType));
        session.close();
        return cutBean;
    }

    public static void main(String[] args) {
        IRoomService service = new RoomServiceImpl();
//        System.out.println(service.del(3));
//        System.out.println(service.findById(5).getList());
//        System.out.println(service.findByItem(null,"不限","可住房间","男生宿舍"));
        System.out.println(service.cutByItem(1,null,"不限","不限","不限"));
    }
}
